package cn.iocoder.yudao.module.album.controller.admin.imgsubject;

import io.swagger.v3.oas.annotations.Hidden;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.album.controller.admin.imgsubject.vo.*;
import cn.iocoder.yudao.module.album.dal.dataobject.imgsubject.ImgSubjectDO;
import cn.iocoder.yudao.module.album.service.imgsubject.ImgSubjectService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

@Tag(name = "管理后台 - 用户相册")
@RestController
@RequestMapping("/album/img-subject")
@Validated
@Hidden
public class ImgSubjectController {

    @Resource
    private ImgSubjectService imgSubjectService;

    @PostMapping("/create")
    @Operation(summary = "创建用户相册")
    @PreAuthorize("@ss.hasPermission('album:img-subject:create')")
    public CommonResult<Long> createImgSubject(@Valid @RequestBody ImgSubjectSaveReqVO createReqVO) {
        return success(imgSubjectService.createImgSubject(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新用户相册")
    @PreAuthorize("@ss.hasPermission('album:img-subject:update')")
    public CommonResult<Boolean> updateImgSubject(@Valid @RequestBody ImgSubjectSaveReqVO updateReqVO) {
        imgSubjectService.updateImgSubject(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除用户相册")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('album:img-subject:delete')")
    public CommonResult<Boolean> deleteImgSubject(@RequestParam("id") Long id) {
        imgSubjectService.deleteImgSubject(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除用户相册")
                @PreAuthorize("@ss.hasPermission('album:img-subject:delete')")
    public CommonResult<Boolean> deleteImgSubjectList(@RequestParam("ids") List<Long> ids) {
        imgSubjectService.deleteImgSubjectListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得用户相册")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('album:img-subject:query')")
    public CommonResult<ImgSubjectRespVO> getImgSubject(@RequestParam("id") Long id) {
        ImgSubjectDO imgSubject = imgSubjectService.getImgSubject(id);
        return success(BeanUtils.toBean(imgSubject, ImgSubjectRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得用户相册分页")
    @PreAuthorize("@ss.hasPermission('album:img-subject:query')")
    public CommonResult<PageResult<ImgSubjectRespVO>> getImgSubjectPage(@Valid ImgSubjectPageReqVO pageReqVO) {
        PageResult<ImgSubjectDO> pageResult = imgSubjectService.getImgSubjectPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ImgSubjectRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出用户相册 Excel")
    @PreAuthorize("@ss.hasPermission('album:img-subject:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportImgSubjectExcel(@Valid ImgSubjectPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ImgSubjectDO> list = imgSubjectService.getImgSubjectPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "用户相册.xls", "数据", ImgSubjectRespVO.class,
                        BeanUtils.toBean(list, ImgSubjectRespVO.class));
    }

}